<?php
global $smarty;

$thisPage = "bulkemails";

$params = unserialize(stripslashes(getRequestParameter("params")));

addDataservice("getbulkusers");
adddataservice("senducmail");
addDataservice("filter");

function buildLayout($smarty) {
    global $thisPage;
    global $params;
    global $titles;

    TemplateHelper::useLayout("default", true);
    $smarty->assign("default", array(
        content => getContent($smarty),
        contexthelp => getContextHelp($smarty),
    ));
}

function executeService($serviceName, $params, $smarty) {
    switch ($serviceName) {
        case "getbulkusers" : { return service_getbulkusers($params); }
        case "senducmail" : { return service_senducmail($params); }
        case "filter" : { return service_filter($params); }
    }
}

function service_getbulkusers($params) {
    Database::loadModel("user");
    $reccount = Database::getModel()->getBulkUserCount();
    $userlist = Database::getModel()->getBulkUsers(getRequestParameter("pg"), getRequestParameter("rows"));
    $limit = getRequestParameter("rows");
    return array(
        page => getRequestParameter("pg"),
        total => ceil($reccount / $limit),
        records => $reccount,
        rows => JQGridHelper::convert($userlist, "user_id"),
    );
}

function getContent($smarty) {
    global $thisPage;
    
    return $smarty->fetch(TemplateHelper::getTemplateFile($thisPage, "content", true));
}

function service_filter($params) {
    Database::loadModel("user");
    $reccount = Database::getModel()->getBulkUserCount();
    $userlist = Database::getModel()->getBulkUsers(1, 50, $params['type']);
    $limit = 50;
    return serviceSuccessful(array(
        griddata => array(
            rows => $userlist,
        ),
    ));
}

function service_sendUCMail($params) {
    Database::loadModel("user");
    $users = Database::getModel()->getUsersForemailsending($params['count'], $params['type']);
    for ($i = 0; $i < count($users); $i++) {
        $users[$i]['password'] = $users[$i]['decryptedpw'];
        sendEmail("usercreated", $users[$i]['email'], $users[$i]);
//        print_r(getEmailText("usercreated", $users[$i]));
    }
    return serviceSuccessful(array());
}

function getcontextHelp() {
}

function getPageHeader() {
}

function getPageFooter() {
}

?>
